// components/agreementPopup/index.js
var http = require('../../utils/http');
var api = require('../../utils/api');
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        showAgreementPopup: Boolean,
        ifvip:Number||String
    },

    /**
     * 组件的初始数据
     */
    data: {
        checked: false
    },

    /**
     * 组件的方法列表
     */
    methods: {
        onChangeCheckBox(event) {
            this.setData({
                checked: event.detail,
            });
        },
        jump1() {
            wx.showLoading({
                title: '打开中...',
            })
            wx.downloadFile({
                url: 'https://www.seemoreopt.com/api/readFiles/UserServe.pdf', //要预览的PDF的地址
                success: function (res) {
                    console.log(res);
                    if (res.statusCode === 200) { //成功
                        var Path = res.tempFilePath //返回的文件临时地址，用于后面打开本地预览所用
                        wx.openDocument({
                            filePath: Path, //要打开的文件路径
                            success: function (res) {
                                console.log('打开PDF成功');
                                wx.hideLoading()
                            }
                        })
                    }
                },
                fail: function (res) {
                    console.log(res); //失败
                }
            })
        },
        jump2() {
            wx.showLoading({
                title: '打开中...',
            })
            wx.downloadFile({
                url: 'https://www.seemoreopt.com/api/readFiles/ChildPrivacy.pdf', //要预览的PDF的地址
                success: function (res) {
                    console.log(res);
                    if (res.statusCode === 200) { //成功
                        var Path = res.tempFilePath //返回的文件临时地址，用于后面打开本地预览所用
                        wx.openDocument({
                            filePath: Path, //要打开的文件路径
                            success: function (res) {
                                console.log('打开PDF成功');
                                wx.hideLoading()
                            }
                        })
                    }
                },
                fail: function (res) {
                    console.log(res); //失败
                }
            })
        },
        jump3() {
            wx.showLoading({
                title: '打开中...',
            })
            wx.downloadFile({
                url: 'https://www.seemoreopt.com/api/readFiles/UserPrivacy.pdf', //要预览的PDF的地址
                success: function (res) {
                    console.log(res);
                    if (res.statusCode === 200) { //成功
                        var Path = res.tempFilePath //返回的文件临时地址，用于后面打开本地预览所用
                        wx.openDocument({
                            filePath: Path, //要打开的文件路径
                            success: function (res) {
                                console.log('打开PDF成功');
                                wx.hideLoading()
                            }
                        })
                    }
                },
                fail: function (res) {
                    console.log(res); //失败
                }
            })
        },
        /**
         * 微信一键登录
         */
        handleWxLogin(e) {
            // console.log( this.data.ifvip);
            this.triggerEvent('iSeeQueryFun', {
                checked: true,
                showAgreementPopup: false,
                loading: true
            })
            wx.login({
                success: (res) => {
                    const code = res.code;
                    http.postHttp(api.wxlogin, {
                            code: code,
                            if_vip: this.data.ifvip == 1 ? true : false
                        },
                        (result) => {
                            if (result.code == 0) {
                                if (e.detail.errMsg == 'getPhoneNumber:ok') {
                                    this.setData({
                                        checked: true
                                    })
                                    this.triggerEvent('iSeeQueryFun', {
                                        checked: true,
                                        showAgreementPopup: false
                                    })
                                    const {
                                        code
                                    } = e.detail
                                    http.postHttp(api.getUserPhone, {
                                            code: code,
                                            openid: result.data.user.openid
                                        },
                                        (res) => {
                                            if (res.code == 0) {
                                                console.log(res);
                                                this.triggerEvent('callBackLogin',{
                                                    result,res
                                                })
                                            } else {
                                                wx.showModal({
                                                    showCancel: false,
                                                    content: res.message,
                                                })
                                            }
                                        },
                                        (err) => {
                                            console.log(err);
                                        }, {
                                            hideLoading: true
                                        }
                                    )
                                } else {
                                    wx.showModal({
                                        showCancel: false,
                                        content: "请允许获取手机号",
                                    })
                                    this.setData({
                                        checked: true,
                                        
                                    })
                                    this.triggerEvent('iSeeQueryFun', {
                                        checked: true,
                                        showAgreementPopup: false,
                                        loading: false
                                    })
                                }

                            } else {
                                wx.showModal({
                                    showCancel: false,
                                    content: result.msg,
                                })
                            }
                        },
                        (err) => {
                            console.log(err);
                        }, {
                            hideLoading: true
                        }
                    )
                },
            })
        },
        iSeeQueryFun(e) {
            console.log(e);
            this.setData({
                checked: true
            })
            this.triggerEvent('iSeeQueryFun', {
                checked: true,
                showAgreementPopup: false
            }, e)
        }
    }
})